package com.seaside.park.api;

import java.util.List;

import net.bytebuddy.implementation.bind.annotation.Default;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seaside.common.bean.Result;
import com.seaside.park.bean.Notice;
import com.seaside.park.mapper.NoticeMapper;
import com.seaside.parkCommon.bean.User;
import com.seaside.parkCommon.mapper.UserMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags="公告展示")
@RestController
@RequestMapping("/api/notice")
public class NoticeAPI {

    @Autowired
    private NoticeMapper noticemapper;

    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "公告列表", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token")
    })
    @PostMapping("/list")
    public Result<List<Notice>> list(String villageId, int curpage, @RequestParam(defaultValue = "10") int limit) {
        Page<Notice> page = new Page<>(curpage, limit, false);

        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("village_id", villageId).orderByDesc("id");
        IPage<Notice> iPage = noticemapper.selectPage(page, queryWrapper);
        Result<List<Notice>> result =  Result.OK("查询成功");
        result.setData(iPage.getRecords());
        return result;
    }

    @ApiOperation(value = "根据ID查看公告信息", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token"),
            @ApiImplicitParam(paramType="query",name="id",dataTypeClass=Integer.class,required=true,value="公告编号")

    })
    @PostMapping("/info")
    public Result<Notice> info(int id) {
        Notice notice = noticemapper.selectById(id);
        Result<Notice> result =  Result.OK("查询成功");
        result.setData(notice);
        return result;
    }
    
    @ApiOperation(value = "公告保存", response = Result.class)
    @ApiImplicitParams({
    	@ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token")
    })
    @PostMapping("/save")
    public Result<Notice> save(@RequestBody Notice notice) {
    	
    	if (notice == null) {
    		return Result.Error("参数错误！");
    	}
    	
    	if (notice.getId() < 1) {
    		noticemapper.insert(notice);
    	} else {
    		noticemapper.updateById(notice);
    	}
    	
        return Result.OK("保存成功！", notice);
    }
}
